package com.oss.mo.controller;

import com.eova.model.MetaField;
import com.eova.model.MetaObject;
import com.eova.service.sm;
import com.eova.widget.WidgetManager;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.oss.model.Hotel;

/**
 * 描述:该controller可以删除
 * 
 * @author gyp
 * @Date 2016年12月12日
 */
public class TestController extends Controller {

	private Hotel hotel;

	/**
	 * 默认路径
	 */
	public void index() {
		System.out
				.println("++++++++++++++++++++++++++++++++++++++++++++这是默认 的方法");

		render("/index.jsp"); // 跳转到jsp页面
	}

	/**
	 * 指定路径,route里的url+/getModel 获取用户Model
	 */
	public void getModel() {
		System.out
				.println("************************************************执行hello的方法");

		Hotel hotel = Hotel.dao.findById(1);
		renderText("Hello JFinal World......");
	}

	public void detail() {
		buildFormData(false);
		render("/eova/widget/form/setUp.html");
	}

	/**
	 * 更新用户案例
	 */
	public void update() {
		Hotel hotel = Hotel.dao.findByUid(1);
		// Hotel hotel = getModel(Hotel.class);
		hotel.set("name", 1223);
		hotel.update();

		forwardAction("/test/getModel");// 跳转到指定的方法内
	}

	private void buildFormData(boolean isEdit) {
		String objectCode = getPara(0);

		Object pkValue = getPara(1);

		MetaObject object = sm.meta.getMeta(objectCode);

		Record record = Db.use(object.getDs()).findById(object.getView(),
				object.getPk(), new Object[] { pkValue });

		for (MetaField ei : object.getFields()) {
			String key = ei.getEn();
			Object value = record.get(key);

			if (value == null) {
				value = "";
			}

			if (!isEdit) {
				ei.set("is_disable", Boolean.valueOf(true));
			}

			ei.put("value", value);
		}

		WidgetManager.buildRef(this, object);

		setAttr("object", object);
		setAttr("fields", object.getFields());
	}
}
